package com.graphql.demo;

import com.graphql.demo.resolver.MetaDataQuery;
import com.linecorp.armeria.common.HttpRequest;
import com.linecorp.armeria.common.HttpResponse;
import com.linecorp.armeria.common.SessionProtocol;
import com.linecorp.armeria.server.Server;
import com.linecorp.armeria.server.ServerBuilder;
import com.linecorp.armeria.server.ServiceRequestContext;
import com.linecorp.armeria.server.annotation.Blocking;
import com.linecorp.armeria.server.annotation.Post;
import com.linecorp.armeria.server.graphql.GraphqlService;
import graphql.analysis.MaxQueryComplexityInstrumentation;
import graphql.kickstart.tools.SchemaParser;
import graphql.kickstart.tools.SchemaParserBuilder;
import graphql.scalars.ExtendedScalars;
import graphql.schema.GraphQLSchema;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

@Slf4j
public class GrapHqlApplication {

//    private static final GraphqlService graphqlService;
//
//    static {
//        SchemaParserBuilder schemaBuilder = SchemaParser.newParser();
//
//        schemaBuilder.file("graphql/user.graphqls")
//                .resolvers(new MetaDataQuery());
//        schemaBuilder.scalars(ExtendedScalars.GraphQLLong);
//        GraphQLSchema schema = schemaBuilder.build().makeExecutableSchema();
//        int allowedComplexity = 1000;
//        graphqlService = GraphqlService
//                .builder()
//                .schema(schema)
//                .instrumentation(new MaxQueryComplexityInstrumentation(allowedComplexity, info -> {
//                    log.warn(
//                            "Aborting query because it's too complex, maximum allowed is [{}] but was [{}]",
//                            allowedComplexity,
//                            info.getComplexity());
//                    return true;
//                }))
//                .build();
//    }

//    @Blocking
//    @Post("/graphql")
//    public HttpResponse graphql(
//            final ServiceRequestContext ctx,
//            final HttpRequest req) throws Exception {
//        return graphqlService.serve(ctx, req);
//    }

    public static void main(String[] args) throws IOException {
        SchemaParserBuilder schemaBuilder = SchemaParser.newParser();

        schemaBuilder.file("graphql/test.graphqls")
                .resolvers(new MetaDataQuery());
        schemaBuilder.scalars(ExtendedScalars.GraphQLLong);
        GraphQLSchema schema = schemaBuilder.build().makeExecutableSchema();
        int allowedComplexity = 1000;
        ServerBuilder serverBuilder = Server.builder();
        GraphqlService graphqlService = GraphqlService
                .builder()
                .schema(schema)
                .instrumentation(new MaxQueryComplexityInstrumentation(allowedComplexity, info -> {
                    log.warn(
                            "Aborting query because it's too complex, maximum allowed is [{}] but was [{}]",
                            allowedComplexity,
                            info.getComplexity());
                    return true;
                }))
                .build();

        serverBuilder.port(8080, SessionProtocol.HTTP);
        serverBuilder.service("/graphql",graphqlService);
        Server server = serverBuilder.build();
        server.start();
    }
}
